package com.terrorism.sdk.photo;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import com.terrorism.sdk.R;
import com.terrorism.sdk.utils.FileUtils;
import com.terrorism.sdk.utils.ScreenUtils;
import com.terrorism.sdk.utils.TimeUtils;
import com.terrorism.sdk.view.CustomToast;

/**
 * @since Monday, June 02, 2014
 * @author hoangcaomobile
 *
 */
public class PhotoProcess {

	// process existed photo and return photo path
	public static String getPathPhotoProcessed(Activity activity, String saveToFolderName, String originPhotoPath) {
		String newPhotoPath = null;
		Log.i(PhotoProcess.class.getSimpleName(), "Photo type: " + FileUtils.getFileType(originPhotoPath));
		if (FileUtils.isSDCardOK(FileUtils.folderMain + "/" + saveToFolderName)) {
			if (FileUtils.getFileType(originPhotoPath).equals("gif") || FileUtils.getFileType(originPhotoPath).endsWith("GIF")) {
				newPhotoPath = originPhotoPath;
			} else {
				File fileNew = new File(FileUtils.folderSystem + "/" + FileUtils.folderMain + "/" + saveToFolderName + "/Photo_"  + TimeUtils.getCurrentDate() + System.currentTimeMillis() + ".jpg");
				FileOutputStream fos;
				try {
					fos = new FileOutputStream(fileNew);					
					BitmapFactory.Options options = new BitmapFactory.Options();
					options.inSampleSize = 2;      // 1/2 of original image

					// resize
					Double fileSize = getFileSize(originPhotoPath);
					if (fileSize >= 5000) {
						options.inSampleSize = 12;
					} else if (fileSize < 5000 && fileSize >= 4000) {
						options.inSampleSize = 10;
					} else if (fileSize < 4000 && fileSize >= 3000) {
						options.inSampleSize = 8;
					} else if (fileSize < 3000 && fileSize >= 2000) {
						options.inSampleSize = 6;
					} else if (fileSize < 2000 && fileSize >= 1000) {
						options.inSampleSize = 4;
					}
					// --

					BitmapFactory.decodeFile(originPhotoPath, options).compress(Bitmap.CompressFormat.JPEG, 100, fos);
					fos.close();
					newPhotoPath =  fileNew + "";

					Log.i("PhotoProcess", "Origin file size: " + Double.toString(fileSize) + " KB"
							+ " | Match resize: " + options.inSampleSize
							+ " | New file size: " + Double.toString(getFileSize(newPhotoPath)) + " KB");
				} catch (FileNotFoundException e) {
				} catch (IOException e) {
				}
			}
		} else {
			CustomToast.vToastLong(activity, activity.getString(R.string.toastErrorSDCard));
		}
		Log.e("PhotoProcess", "New photo path: " + newPhotoPath);
		return newPhotoPath;
	}

	public static String getPathPhotoProcessed(Activity mActivity, String mFolderParent, String mFolderChildren, String mOriginPhotoPath) {
		String newPhotoPath = null;
		Log.i(PhotoProcess.class.getSimpleName(), "Photo type: " + FileUtils.getFileType(mOriginPhotoPath));
		if (FileUtils.isSDCardOK(FileUtils.folderMain + "/" + mFolderChildren)) {
			if (FileUtils.getFileType(mOriginPhotoPath).equals("gif") || FileUtils.getFileType(mOriginPhotoPath).endsWith("GIF")) {
				newPhotoPath = mOriginPhotoPath;
			} else {
				File fileNew = new File(FileUtils.folderSystem + "/" + mFolderParent + "/" + mFolderChildren + "/" +
						TimeUtils.getCurrentDate() + "_"  + System.currentTimeMillis() + ".jpg");
				FileOutputStream fos;
				try {
					fos = new FileOutputStream(fileNew);					
					BitmapFactory.Options options = new BitmapFactory.Options();
					options.inSampleSize = 2;      // 1/2 of original image

					// resize
					Double fileSize = getFileSize(mOriginPhotoPath);
					if (fileSize >= 5000) {
						options.inSampleSize = 12;
					} else if (fileSize < 5000 && fileSize >= 4000) {
						options.inSampleSize = 10;
					} else if (fileSize < 4000 && fileSize >= 3000) {
						options.inSampleSize = 8;
					} else if (fileSize < 3000 && fileSize >= 2000) {
						options.inSampleSize = 6;
					} else if (fileSize < 2000 && fileSize >= 1000) {
						options.inSampleSize = 4;
					}
					// --

					BitmapFactory.decodeFile(mOriginPhotoPath, options).compress(Bitmap.CompressFormat.JPEG, 100, fos);
					fos.close();
					newPhotoPath =  fileNew + "";

					Log.i("PhotoProcess", "Origin file size: " + Double.toString(fileSize) + " KB"
							+ " | Match resize: " + options.inSampleSize
							+ " | New file size: " + Double.toString(getFileSize(newPhotoPath)) + " KB");
				} catch (FileNotFoundException e) {
				} catch (IOException e) {
				}
			}
		} else {
			CustomToast.vToastLong(mActivity, mActivity.getString(R.string.toastErrorSDCard));
		}
		Log.e("PhotoProcess", "New photo path: " + newPhotoPath);
		return newPhotoPath;
	}
	// --

	// process bitmap and return photo path
	public static String getPathBitmapProcessed(Activity activity, String saveToFolderName, Bitmap bitmap) {
		String newPhotoPath = null;
		if (FileUtils.isSDCardOK(FileUtils.folderMain + "/" + saveToFolderName)) {
			File fileNew = new File(FileUtils.folderSystem + "/" + FileUtils.folderMain + "/" + saveToFolderName + "/Photo_"  + TimeUtils.getCurrentDate() + System.currentTimeMillis() + ".jpg");
			FileOutputStream fos;
			try {
				fos = new FileOutputStream(fileNew);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
				fos.close();
				newPhotoPath =  fileNew + "";
			} catch (FileNotFoundException e) {
			} catch (IOException e) {
			}
		} else {
			CustomToast.vToastLong(activity, activity.getString(R.string.toastErrorSDCard));
		}
		return newPhotoPath;
	}
	// --

	// process bitmap and return photo path for wall and avatar
	public static String getPathBitmapProcessedForWallAvatar(Activity activity, String saveToFolderName, Bitmap bitmap) {
		String newPhotoPath = null;
		if (FileUtils.isSDCardOK(FileUtils.folderMain + "/" + saveToFolderName)) {
			File fileNew = new File(FileUtils.folderSystem + "/" + FileUtils.folderMain + "/" + saveToFolderName + "/Photo_"  + TimeUtils.getCurrentDate() + System.currentTimeMillis() + ".jpg");
			FileOutputStream fos;
			try {
				fos = new FileOutputStream(fileNew);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 99, fos);
				fos.close();
				newPhotoPath =  fileNew + "";
			} catch (FileNotFoundException e) {
			} catch (IOException e) {
			}
		} else {
			CustomToast.vToastLong(activity, activity.getString(R.string.toastErrorSDCard));
		}
		return newPhotoPath;
	}
	// --

	// get file zise
	private static double getFileSize(String filePath) {
		File file = new File(filePath);
		double fileSize = 0;
		if (file.isFile()) {
			fileSize = file.length() / 1024;
		}
		return fileSize;
	}   
	// --

	// get bitmap thumbnail via screen width
	public static Bitmap getThumbnailSquare(Activity activity, Bitmap bitmap, int divideScreenWidth) {
		Bitmap newBitmap = ThumbnailUtils.extractThumbnail(bitmap,
				(ScreenUtils.getScreenWidth(activity) / divideScreenWidth),
				(ScreenUtils.getScreenWidth(activity) / divideScreenWidth));
		return newBitmap;
	}
	// --

	// get real photo path from Uri
	public static String getRealPhotoPathFromURI(Context context, Uri contentURI) {
		Cursor cursor = context.getContentResolver()
				.query(contentURI, null, null, null, null); 
		cursor.moveToFirst(); 
		int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
		System.out.println("idx = "+idx);
		if (idx == -1){return new String("null");}

		return cursor.getString(idx); 
	}
	// --

}
